//
//  HomeConvenientViewController.m
//  CommunitySupermarketProiect
//
//  Created by 吕惠卿 on 2017/10/18.
//  Copyright © 2017年 吕惠卿. All rights reserved.
//
#define NAVBAR_COLORCHANGE_POINT (-IMAGE_HEIGHT + NAV_HEIGHT)
#define NAV_HEIGHT 64
#define IMAGE_HEIGHT 200
#define SCROLL_DOWN_LIMIT 70
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define LIMIT_OFFSET_Y -(IMAGE_HEIGHT + SCROLL_DOWN_LIMIT)



#import "HomeConvenientViewController.h"
#import "SDCycleScrollView.h"
#import "WRImageHelper.h"
#import "WRNavigationBar.h"
@interface HomeConvenientViewController ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

{
    NSArray *onearr;
    NSArray *fenphoarr;
}

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UIImageView *advView;

@end

static NSString *homefirstColl = @"homefirstColl";
static NSString *lineColl = @"lineColl";
static NSString *ConveColl = @"ConveColl";
static NSString *homesecondColl = @"homesecondColl";

static NSString *homethirdColl = @"homethirdColl";
static NSString *homefourColl = @"homefourColl";
static NSString *homefiveColl = @"homefiveColl";
static NSString *homesixColl = @"homesixColl";
static NSString *homesevenColl = @"homesevenColl";
static NSString *homeeightColl = @"homeeightColl";
static NSString *homenineColl = @"homenineColl";



@implementation HomeConvenientViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NAVIGATIONTITLE(@"便捷购物");
    WhiteColor;
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self creatNavigationItem];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//
//    self.collectionView.contentInset = UIEdgeInsetsMake(IMAGE_HEIGHT-64, 0, 0, 0);
    [self.collectionView addSubview:self.advView];
    [self.view addSubview:self.collectionView];

//    onearr = @[@"汽车",@"服装",@"箱包",@"百货",@"家电",@"美妆",@"家居",@"分类"];
//    fenphoarr = @[@"汽车",@"服装",@"箱包",@"百货",@"家电",@"美妆",@"家居",@"分类"];
   

}

#pragma mark -UICollectionViewDelegate
-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //背景颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        //自适应大小
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[HomeFirstCollectionViewCell class] forCellWithReuseIdentifier:@"homefirstColl"];
        [_collectionView registerClass:[LineGrayCollectionViewCell class] forCellWithReuseIdentifier:@"lineColl"];
        [_collectionView registerClass:[HomeConvenientCollectionViewCell class] forCellWithReuseIdentifier:@"ConveColl"];
        
        
        
        
        [_collectionView registerClass:[HomeSecondCollectionViewCell class] forCellWithReuseIdentifier:@"homesecondColl"];
        [_collectionView registerClass:[HomeThirdCollectionViewCell class] forCellWithReuseIdentifier:@"homethirdColl"];
        [_collectionView registerClass:[HomeFourCollectionViewCell class] forCellWithReuseIdentifier:@"homefourColl"];
        [_collectionView registerClass:[HomeFiveCollectionViewCell class] forCellWithReuseIdentifier:@"homefiveColl"];
        [_collectionView registerClass:[HomeSixCollectionViewCell class] forCellWithReuseIdentifier:@"homesixColl"];
        [_collectionView registerClass:[HomeSevenCollectionViewCell class] forCellWithReuseIdentifier:@"homesevenColl"];
        [_collectionView registerClass:[HomeEightCollectionViewCell class] forCellWithReuseIdentifier:@"homeeightColl"];
        [_collectionView registerClass:[HomeNineCollectionViewCell class] forCellWithReuseIdentifier:@"homenineColl"];
        
    }
    return _collectionView;
}
//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HomeFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefirstColl forIndexPath:indexPath];
            cell.img.image = [[UIImage imageNamed:fenphoarr[indexPath.row]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            cell.lab.text = onearr[indexPath.item];
       
        
        return cell;
        
    }
    else if (indexPath.section == 1){
        LineGrayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:lineColl forIndexPath:indexPath];
        return cell;
    }
    else if (indexPath.section == 2){
        HomeFiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefiveColl forIndexPath:indexPath];
        //        cell.advImg.image = [UIImage imageNamed:@"广告"];
        cell.nameLab.text = @"名牌秒杀";
        cell.englishLab.text = @"距离开始";
        return cell;
 
    }
    else if (indexPath.section == 3){
        HomeConvenientCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ConveColl forIndexPath:indexPath];
//        cell.backgroundColor = [UIColor redColor];
        cell.imageView.image = [UIImage imageNamed:@"秒杀图片"];
        cell.nameLab.text = @"VANS/烦死男鞋STYLE 36板鞋白蓝白红GD女鞋休闲帆布";
        cell.xianmoneyLab.text = @"￥599";
        
        return cell;
    }
    else if (indexPath.section == 4){
        LineGrayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:lineColl forIndexPath:indexPath];
        return cell;
            }
    else if (indexPath.section == 5){
        HomeSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homesecondColl forIndexPath:indexPath];
        cell.rightImg.image = [UIImage imageNamed:@"免费-入驻"];
        return cell;

    }
    else if (indexPath.section == 6){
        HomeThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homethirdColl forIndexPath:indexPath];
        cell.layer.borderWidth = 0.3;
        cell.layer.borderColor = [UIColor colorWithRed:236.0/255.0 green:236.0/255.0 blue:236.0/255.0 alpha:1.0].CGColor;
        if (indexPath.item == 0) {
            cell.rightImg.image = [UIImage imageNamed:@"专题"];
            cell.bigLab.text = @"专题活动";
            cell.contLab.text = @"品牌上新";
        }
        else if (indexPath.item == 1){
            cell.rightImg.image = [UIImage imageNamed:@"聚餐"];
            cell.bigLab.text = @"年底聚餐";
            cell.contLab.text = @"品牌上新";
        }
        else if (indexPath.item == 2){
            cell.rightImg.image = [UIImage imageNamed:@"秒杀专区"];
            cell.bigLab.text = @"秒杀专区";
            cell.contLab.text = @"品牌上新";
        }
        else{
            cell.rightImg.image = [UIImage imageNamed:@"推荐有奖"];
            cell.bigLab.text = @"推荐有奖";
            cell.contLab.text = @"品牌上新";
            
        }
        return cell;

    }
    else if (indexPath.section == 7){
        LineGrayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:lineColl forIndexPath:indexPath];
        return cell;
    }
    else if (indexPath.section == 8){
        HomeFourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homefourColl forIndexPath:indexPath];
        cell.backgroundColor = LineColor;
        cell.advImg.image = [UIImage imageNamed:@"广告"];
        return cell;
    }
    else if (indexPath.section == 9){
        HomeEightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeeightColl forIndexPath:indexPath];
        NSString *words = @"——  猜你喜欢 ——";
        NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]}];
        NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        attatch.bounds = CGRectMake(0, 0, 13, 13);
        attatch.image = [UIImage imageNamed:@"猜你喜欢"];
        NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch];
        [strAtt insertAttributedString:string8 atIndex:3];
        cell.label.attributedText = strAtt;
        return cell;
    }
    else{
        HomeNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homenineColl forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:@"猜你喜欢商品图"];
        cell.nameLab.text = @"VANS/烦死男鞋STYLE 36板鞋白蓝白红GD女鞋休闲帆布";
        cell.moneyLab.text = @"￥599";
        cell.numLab.text = @"820人已购买";
        return cell;
    }
}
//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 11;
}
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 8;
    }
    else if (section == 1){
        return 1;
    }
    else if (section == 2){
        return 1;
    }
    else if (section == 3){
        return 3;
    }
    else if (section == 4){
        return 1;
    }
    else if (section == 5){
        return 1;
    }
    else if (section == 6){
        return 4;
    }
    else if (section == 7){
        return 1;
    }
    else if (section == 8){
        return 1;
    }
    else if (section == 9){
        return 1;
    }
    else{
        return 6;
    }
    
}
//机会选中消息
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  YES;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return (CGSize){MCScreenSize.width/4 , 95};
    }
    else if (indexPath.section == 1){
        return (CGSize){MCScreenSize.width , 5};
    }
    else if (indexPath.section == 2){
        return (CGSize){MCScreenSize.width , 40};
    }
    else if (indexPath.section == 3){
        return (CGSize){MCScreenSize.width/3 , 170};
    }
    else if (indexPath.section == 4){
        return (CGSize){MCScreenSize.width , 5};
    }
    else if (indexPath.section == 5){
        return (CGSize){MCScreenSize.width , 90};
    }
    else if (indexPath.section == 6){
        return (CGSize){MCScreenSize.width/2 , 70};
    }
    else if (indexPath.section == 7){
        return (CGSize){MCScreenSize.width , 5};
    }
    else if (indexPath.section == 8){
        return (CGSize){MCScreenSize.width , 90};
    }
    else if (indexPath.section == 9){
        return (CGSize){MCScreenSize.width , 40};
    }
    else{
        return (CGSize){MCScreenSize.width/2 , 200};
    }
    
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductDetailsViewController *provc = [[ProductDetailsViewController alloc]init];
    provc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:provc animated:YES];
//    if (indexPath.section == 0) {
//
//    }
//    else if (indexPath.section == 1){
//
//    }
//    else if (indexPath.section == 2){
//
//    }
//    else if (indexPath.section == 3){
//
//    }
//    else if (indexPath.section == 4){
//
//    }
//    else if (indexPath.section == 5){
//
//    }
//    else if (indexPath.section == 6){
//
//    }
//    else if (indexPath.section == 7){
//
//    }
//    else if (indexPath.section == 8){
//
//    }
//    else{
//
//    }
}



- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width*2, newSize.height*2));
    [image drawInRect:CGRectMake (0, 0, newSize.width*2, newSize.height*2)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

-(UIImageView *)advView{
    if (_advView == nil) {
        _advView = [[UIImageView alloc]init];
        _advView.frame = CGRectMake(0, 0, kScreenWidth, IMAGE_HEIGHT);
        _advView.image = [UIImage imageNamed:@"首页banner"];
    }
    return _advView;
}
-(void)creatNavigationItem{
    UIButton *btn = [UIButton new];
    //    btn.backgroundColor = [UIColor redColor];
    [btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void)btnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
